package com.example.administrator.gongxiangcheng;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import base.BaseFragment;
import fragment.HomeFragment;
import fragment.InitiateFragment;
import fragment.MineFragment;
import fragment.NearbyFragment;
import fragment.OrderFragment;

public class MainActivity extends FragmentActivity {
    private RadioGroup radioGroup;
    private List<BaseFragment> mBaseFragment;
    //选择Fragment对应的位置
    private int position;
    //上次切换的Fragment
    private Fragment mContent;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化view
        initView();
        //初始化Fragment
        initFragment();
        //设置RadioGroup的监听事件
        setListener();
    }
    private void initFragment() {
        mBaseFragment = new ArrayList<>();
        mBaseFragment.add(new HomeFragment());
        mBaseFragment.add(new InitiateFragment());
        mBaseFragment.add(new MineFragment());
        mBaseFragment.add(new NearbyFragment());
        mBaseFragment.add(new OrderFragment());
    }

    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
    }

    private void setListener() {
        radioGroup.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        //设置默认选中常用框架
        radioGroup.check(R.id.rb_initiate_the_raise);
    }

    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_home:
                    position = 0;
                    break;
                case R.id.rb_nearby_stores:
                    position = 1;
                    break;
                case R.id.rb_initiate_the_raise:
                    position = 2;
                    break;
                case R.id.rb_order:
                    position = 3;
                    break;
                case R.id.rb_mine:
                    position = 4;
                    break;
                default:
                    position = 0;
                    break;


            }
            //根据位置得到对应的Fragment
            BaseFragment to= getFragment();
            //替换
            switchFragment(mContent,to);

        }

        private void switchFragment(Fragment from,Fragment to) {
            if (from != to) {
                mContent = to;
                FragmentTransaction ft= getSupportFragmentManager().beginTransaction();

                //才切换
                //判断有没有被添加
                if (!to.isAdded()) {
                    //to没有被添加
                    //from隐藏
                    if (from !=null){
                        ft.hide(from);
                    }
                    //添加to
                    if (to !=null){
                        ft.add(R.id.fl1,to).commit();
                    }

                } else {
                    //to已经被添加
                    //from隐藏
                    if (from !=null){
                        ft.hide(from);
                    }
                    //显示to
                    if (to !=null){
                        ft.show(to).commit();

                    }
                }
            }
        }
        //根据位置得到对应的Fragment
        private BaseFragment getFragment() {
            BaseFragment fragment   =  mBaseFragment.get(position);

            return fragment;
        }
    }
}